package com.hk.common.shiro.oauth;

import com.alibaba.fastjson2.JSONObject;
import com.hk.common.constant.Constants;
import com.hk.common.redis.Redis;
import com.hk.common.shiro.token.JwtToken;
import com.hk.common.shiro.token.JwtTokenUtil;
import com.hk.modules.user.pojo.UserPojo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * 认证
 *
 * @author wujie
 */
@Component
public class OAuthRealm extends AuthorizingRealm {

    private Logger log = LoggerFactory.getLogger(OAuthRealm.class);

    @Autowired
    private Redis redis;

    @Autowired
    private JwtTokenUtil tokenUtil;

    /**
     * 必须重写此方法，不然Shiro会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 登录验证
     * @param authcToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {

        String token = (String) authcToken.getPrincipal();
        if (token == null) {
            throw new AuthenticationException("token为空!");
        }
        // 解密获得username，用于和数据库进行对比
        String username = tokenUtil.getUserName(token);
        if(username == null){
            throw new AuthenticationException("token 无效");
        }
        UserPojo user = redis.getObject(redis.prefixKey(Constants.USER_PREFIX,username));
        if (user == null) {
            throw new AuthenticationException("用户未登录");
        }
        //校验 如果过期则直接更新新的token
        if(tokenUtil.verify(token,user.getUsername())==null){
            throw new AuthenticationException("校验不通过");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, token, getName());

        return info;
    }


    /**
     * 权限验证
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Session session = SecurityUtils.getSubject().getSession();
        //查询用户的权限
        JSONObject permission = (JSONObject) session.getAttribute(Constants.SESSION_USER_PERMISSION);
        log.info("permission的值为:" + permission);
        log.info("本用户权限为:" + permission.get("permissionList"));
        //为当前用户设置角色和权限
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addStringPermissions((Collection<String>) permission.get("permissionList"));
        return authorizationInfo;
    }
}
